function getdatas() {
  var selected = $("#gd").datalist('getChecked');
  var stions = $("#gd").datalist('getSelections');
  console.log(selected);
  console.log(stions);
}

function refresh() {
  $("#gd").datalist('reload');
}

var editIndex = undefined;

function endEditing() {
  if (editIndex == undefined) {
    return true
  }
  if ($('#dg').datagrid('validateRow', editIndex)) {
    $('#dg').datagrid('endEdit', editIndex);
    editIndex = undefined;
    return true;
  } else {
    return false;
  }
}

function onClickCell(index, field) {
  if (editIndex != index) {
    if (endEditing()) {
      $('#dg').datagrid('selectRow', index)
        .datagrid('beginEdit', index);
      var ed = $('#dg').datagrid('getEditor', {
        index: index,
        field: field
      });
      if (ed) {
        ($(ed.target).data('textbox') ? $(ed.target).textbox('textbox') : $(ed.target)).focus();
      }
      editIndex = index;
    } else {
      setTimeout(function () {
        $('#dg').datagrid('selectRow', editIndex);
      }, 0);
    }
  }
}

function onEndEdit(index, row) {
        var ed = $(this).datagrid('getEditor', {
            index: index,
    field: 'medicineName'
  });
  row.medicineName = $(ed.target).combobox('getText');
}

function append() {
  if (endEditing()) {
    $('#dg').datagrid('appendRow', {
      status: 'P'
    });
    editIndex = $('#dg').datagrid('getRows').length - 1;
    $('#dg').datagrid('selectRow', editIndex)
      .datagrid('beginEdit', editIndex);
  }
}

function removeit() {
  if (editIndex == undefined) {
    return
  }
  $('#dg').datagrid('cancelEdit', editIndex)
    .datagrid('deleteRow', editIndex);
  editIndex = undefined;
}

function confirm() {
  $.messager.confirm('My Title', 'Are you confirm this?', function (r) {
    if (r) {
      alert('confirmed: ' + r);
    }
  });
}

function accept() {
  if (endEditing()) {
    $('#dg').datagrid('acceptChanges');
  }
  var rows = $('#dg').datagrid('getRows'); //get all datas in datagrid
  var jsondata = JSON.stringify(rows); //change to json string

}

function reject() {
  $('#dg').datagrid('rejectChanges');
  editIndex = undefined;
}

function getChanges() {
  var rows = $('#dg').datagrid('getChanges');
  alert(rows.length + ' rows are changed!');
}